#include "dl.h"

#include <assert.h>

C_CODE_BEGIN

EXPORT_FUNC dl_handler dl_open( const char* name ) {
#ifdef __linux__
	/*
	 * On Linux , I simply use RTLD_GLOBAL and RTLD_NOW 
	 * the reason I use GLOBAL is because this option make
	 * the dl function fits into different application environment
	 * instead of simple plug-in use
	 */
	return dlopen(name,RTLD_GLOBAL | RTLD_NOW );
#elif _WIN32
	return LoadLibraryA( name );
#else
#endif
}

EXPORT_FUNC void* dl_find_symbol( dl_handler h , const char* name ) {
#ifdef __linux__
	return dlsym( h , name );
#elif _WIN32
	return GetProcAddress( h , name );
#else
#endif

}

EXPORT_FUNC void dl_close( dl_handler h ) {
#ifdef __linux__
	int ret = dlclose(h);
	assert(ret == 0);
#elif _WIN32
	BOOL ret = FreeLibrary(h);
	assert(ret == TRUE);
#else
#endif
}

C_CODE_END